<template>
  <div class="footprint-accounting-content">
    <slot name="default" :hidden-line="false"></slot>
    <AccountingBaseInfo
      :requestNo="props.businessKey"
      @loaded="handleBaseInfoLoaded"
      v-if="props.businessKey"
    />
    <AccountingAuditInfo
      :requestNo="props.businessKey"
      :modelBaseInfo="modelBaseInfo"
      v-if="props.businessKey && readonly && modelBaseInfo"
    />
    <AccountingForm
      ref="accountingFormRef"
      :requestNo="props.businessKey"
      :modelBaseInfo="modelBaseInfo"
      v-else-if="props.businessKey && !readonly && modelBaseInfo"
    />
  </div>
</template>
<script lang="ts">
export default {
  name: 'FootprintAccounting'
};
</script>
<script lang="ts" setup>
import { ref, defineExpose, defineProps, inject, computed } from 'vue';
import AccountingBaseInfo from './components/AccountingBaseInfo/index.vue';
import AccountingAuditInfo from './components/AccountingAuditInfo/index.vue';
import AccountingForm from './components/AccountingForm/index.vue';
import type { TodoDetailRootProvider } from '../../types';
import { TODO_DETAIL_ROOT } from '../../types';
import { TodoTaskStatusEnum, TaskDefinitionKeyCooperationEnum } from '@/constants/enum';
import type { GetRequestInfoResult } from '@/apis/todo/model/flowBusinessModel';

interface FootprintAccountingProps {
  businessKey: string;
}
const props = defineProps<FootprintAccountingProps>();

const { pageType, processInfo } = inject(TODO_DETAIL_ROOT) as TodoDetailRootProvider;
const accountingFormRef = ref();

// NOTE 可以操作的状态
const canOperate = computed(() => {
  // 待办、即将超时、已超时
  return ['PENDING', 'ABOUT_TO_EXPIRE', 'OUT_OF_TIME'].includes(pageType);
});

// 展示查看组件
const readonly = computed(() => {
  // NOTE 仅回复模型需要填写表单信息
  return (
    !canOperate.value ||
    processInfo.value?.taskDefinitionKey !== TaskDefinitionKeyCooperationEnum.CM_REPLY_MODEL
  );
});
const modelBaseInfo = ref<GetRequestInfoResult>();
function handleBaseInfoLoaded(baseInfo: GetRequestInfoResult) {
  if (baseInfo) {
    modelBaseInfo.value = baseInfo;
  }
}

function submit() {
  return accountingFormRef.value?.submit();
}

defineExpose({
  submit
});
</script>
<style lang="scss" scoped>
.footprint-accounting-content {
  padding: 12px 24px 70px;
}
</style>
